import { userRegister } from '../../apis/index.js';
import { validateParam } from '../../validate/validate.js';

Page({
  data: {
    username: "",
    password: "",
    password2: "",
    last_name: "",
    email: "",
    typepd: "info",
    pdcolor: "#ff4d4d",
    typeuname: "info",
    unamecolor: "#ff4d4d",
    typepd2: "info",
    pdcolor2: "#ff4d4d",
    typelname: "info",
    lnamecolor: "#ff4d4d",
    typeemail: "info",
    emailcolor: "#ff4d4d",
    isshow:"显示",
    pdtype:"password"
  },
  isShow:function(){
    const isshow = this.data.pdtype === "password" 
    this.setData({
      isshow:isshow ? "隐藏":"显示",
      pdtype:isshow ? "safe-password":"password"
    })
  },
  handleUname(e) {
    const value = e.detail.value;
    this.setData({
      username: value,
      typeuname: value.length > 0 ? "success" : "info",
      unamecolor: value.length > 0 ? "#00b26a" : "#ff4d4d"
    });
  },

  handlePd(e) {
    const value = e.detail.value;
    this.setData({
      password: value,
      typepd: value.length > 7 ? "success" : "info",
      pdcolor: value.length > 7 ? "#00b26a" : "#ff4d4d"
    });
  },

  handlePd2(e) {
    const value = e.detail.value;
    this.setData({
      password2: value,
      typepd2: (value.length > 7 && value === this.data.password) ? "success" : "info",
      pdcolor2: (value.length > 7 && value === this.data.password) ? "#00b26a" : "#ff4d4d"
    });
  },

  handleLname(e) {
    const value = e.detail.value;
    this.setData({
      last_name: value,
      typelname: value.length > 0 ? "success" : "info",
      lnamecolor: value.length > 0 ? "#00b26a" : "#ff4d4d"
    });
  },

  handleEmail(e) {
    const value = e.detail.value;
    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    const isValidEmail = emailPattern.test(value);
    this.setData({
      email: value,
      typeemail: (value.length > 0 && isValidEmail) ? "success" : "info",
      emailcolor: (value.length > 0 && isValidEmail) ? "#00b26a" : "#ff4d4d"
    });
  },

  userRegister() {
    console.log("注册");
    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    const { username, password, password2, last_name, email } = this.data;
    const isValidEmail = emailPattern.test(email);

    if (!validateParam({ username, password, password2, last_name, email })) return;

    if (password !== password2) {
      wx.showToast({
        title: '两次密码输入不一致',
        icon: "none"
      });
      return;
    }

    if (!isValidEmail) {
      wx.showToast({
        title: '邮箱格式不正确',
        icon: "none"
      });
      return;
    }

    userRegister(username, password, password2, last_name, email)
      .then((res) => {
        console.log("成功");
        console.log(res);
        // 显示成功提示框
        wx.showToast({
          title: '注册成功',
          icon: "success",
          duration: 2000, // 提示框显示时间
        });
        // 在提示框显示后进行页面跳转
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/login/login'
          });
        }, 2000); // 等待提示框消失后再跳转
      })
      .catch((error) => {
        console.log("错误");
        console.log(error);
        const { statusCode, data } = error;
        let errmsg = [];
        for (const key in data) {
          if (data.hasOwnProperty(key)) {
            let messages = data[key];
            messages.forEach(message => {
              errmsg.push(`${key}错误: ${message}`);
            });
          }
        }
        if (errmsg.length > 0) {
          wx.showToast({
            title: errmsg.join('\n'),
            icon: "none"
          });
        } else {
          wx.showToast({
            title: '网络错误请稍后再次请求',
            icon: "none"
          });
        }
      });
  },

  // 可以删除不需要的生命周期函数
  // onLoad(options) { },
  // onReady() { },
  // onShow() { },
  // onHide() { },
  // onUnload() { },
  // onPullDownRefresh() { },
  // onReachBottom() { },
  // onShareAppMessage() { }
});
